package com.java.androidmarket.service.impl;

import com.java.androidmarket.dao.KillStockDao;
import com.java.androidmarket.entity.KillStock;
import com.java.androidmarket.entity.OrderGoods;
import com.java.androidmarket.entity.Stock;
import com.java.androidmarket.exception.ApiException;
import com.java.androidmarket.service.KillStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * description:
 * author:张腾
 * date:2021-06-01
 */

@Service
public class KillStockServiceImpl implements KillStockService {

    //get check set
    @Autowired
    private KillStockDao killStockDao;

    @Override
    public KillStock getStock(int killgoodsId) {
        return killStockDao.getStock(killgoodsId);
    }

    @Override
    public boolean checkStock(int killgoodsId, int wantNums) {

        KillStock stock = killStockDao.getStock(killgoodsId);
        int realStock = stock.getStock()-stock.getSale()-stock.getLock();

        if (wantNums>realStock){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public int setLock(int killgoodsId, int lock,int version) {
        KillStock killStock = new KillStock();
        killStock.setKillgoodsId(killgoodsId).setLock(lock).setVersion(version);
        return  killStockDao.setLock(killStock);
    }


    @Override
    public void lockToSale(OrderGoods orderGoods) {
        KillStock stock = new KillStock();
        stock.setKillgoodsId(orderGoods.getGoodsId()).setSale(orderGoods.getGoodsNum()).setLock(orderGoods.getGoodsNum());

        killStockDao.lockTosSale(stock);
    }

    @Override
    public void reLock(OrderGoods orderGoods){
        killStockDao.reLock(orderGoods.getGoodsId(),orderGoods.getGoodsNum());
    }

    @Override
    public int getRealStock(int killgoodsId) {
        KillStock stock = killStockDao.getStock(killgoodsId);
        return stock.getStock()-stock.getSale()-stock.getLock();
    }


}
